package com.yx.changdao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.entity.SysUser;
import com.yx.changdao.common.entity.WithinLetter;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.mapper.WithinLetterMapper;
import com.yx.changdao.service.SysUserService;
import com.yx.changdao.service.WithinLetterService;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import org.nutz.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: <br/>
 * 栈内信
 * <p>
 * <br/>
 * @Auther: walter
 * @create 2020/05/07 10:34
 */
@Service
public class WithinLetterServiceImpl extends ServiceImpl<WithinLetterMapper, WithinLetter> implements WithinLetterService {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private WithinLetterService withinLetterService;

    @Override
    public boolean sendToOne(String sender, String receiver, String title, String content,Integer type)throws AlertException {
        if(Strings.isBlank(sender)||Strings.isBlank(receiver)||Strings.isBlank(title)||Strings.isBlank(content)||type==null){
            throw new AlertException("存在空字段");
        }
        SysUser sendUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq(SysUser.USER_ID, sender));
        if(sendUser==null){
            throw new AlertException("发送者不存在");
        }
        SysUser receiveUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq(SysUser.USER_ID, receiver));
        if(receiveUser==null){
            throw new AlertException("接收者不存在");
        }
        saveSend(title,content,sendUser.getUserId(),sendUser.getUsername(),
                receiveUser.getUserId(),receiveUser.getUsername(),type);
        return true;
    }

    @Override
    public boolean sendToDep(String sender, Integer depId, String title, String content,Integer type) throws AlertException {
        if(Strings.isBlank(sender)||depId==null||Strings.isBlank(title)||Strings.isBlank(content)||type==null){
            throw new AlertException("存在空字段");
        }
        SysUser sendUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq(SysUser.USER_ID, sender));
        if(sendUser==null){
            throw new AlertException("发送者不存在");
        }
        List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().eq(SysUser.DEP_ID, depId));
        if(list.size()==0){
            throw new AlertException("接收者不存在");
        }
        for (SysUser sysUser : list) {
            saveSend(title,content,sendUser.getUserId(),sendUser.getUsername(),
                    sysUser.getUserId(),sysUser.getUsername(),type);
        }
        return true;
    }

    @Override
    public boolean sendToAll(String sender, String title, String content,Integer type) throws AlertException {
        if(Strings.isBlank(sender)||Strings.isBlank(title)||Strings.isBlank(content)||type==null){
            throw new AlertException("存在空字段");
        }
        SysUser sendUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq(SysUser.USER_ID, sender));
        if(sendUser==null){
            throw new AlertException("发送者不存在");
        }
        List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().ne(SysUser.DEP_ID, sender));
        if(list.size()==0){
            throw new AlertException("接收者不存在");
        }
        for (SysUser sysUser : list) {
            //新增
            saveSend(title,content,sendUser.getUserId(),sendUser.getUsername(),
                    sysUser.getUserId(),sysUser.getUsername(),type);
        }
        return true;
    }

    public boolean saveSend( String title, String content,String sender,String senderName,
                             String receiveId,String receiveName,Integer type) throws AlertException {
        WithinLetter withinLetter = new WithinLetter();
        withinLetter.setTitle(title);
        withinLetter.setContant(content);
        withinLetter.setSendId(sender);
        withinLetter.setSendName(senderName);
        withinLetter.setReceiveId(receiveId);
        withinLetter.setReceiveName(receiveName);
        withinLetter.setType(type);
        boolean save = withinLetterService.save(withinLetter);
        if(!save){
            throw new AlertException("发送失败");
        }
        return true;
    }
}
